import { FormCard } from '@/model/types/formCard';
import {
  IsEmailAvailable,
  IsIDCardAvailable,
  IsPhoneAvailable,
  GetAllCard,
} from '@/global/util/common';
import CardLayout from '@/components/card/CardLayout.vue';
import CardInput from '@/components/card/CardInput.vue';
import CardTextarea from '@/components/card/CardTextarea.vue';
import CardDatetime from '@/components/card/CardDatetime.vue';
import CardInputNumber from '@/components/card/CardInputNumber.vue';
import CardGroupTitle from '@/components/card/CardGroupTitle.vue';
import CardSystemTime from '@/components/card/CardSystemTime.vue';
import CardCreater from '@/components/card/CardCreater.vue';

export const GetCardComponent = (card: FormCard) => {
  const { cardKey } = card;
  switch (cardKey) {
    case 'formLayout':
      return CardLayout;
    case 'formTextBox':
      return CardInput;
    case 'formTextArea':
      return CardTextarea;
    case 'formDatetime':
      return CardDatetime;
    case 'formNumber':
      return CardInputNumber;
    case 'formGroupTitle':
      return CardGroupTitle;
    case 'formCreateTime':
    case 'formUpdateTime':
      return CardSystemTime;
    case 'formCreater':
      return CardCreater;
    default:
      break;
  }
};

export const FormatControlValue = (card: FormCard, val: any) => {
  const { cardKey } = card;
  switch (cardKey) {
    case 'formTextBox':
      const { defaultValue } = card.controls.dataFormat;
      if (!val) return true;
      switch (defaultValue) {
        case 'text':
          return true;
        case 'email':
          return IsEmailAvailable(val);
        case 'IDCard':
          return IsIDCardAvailable(val);
        case 'phone':
          return IsPhoneAvailable(val);
        default:
          return true;
      }
    case 'formDatetime':
      return card.controls.dataFormat;
    case 'formNumber':
      return card.controls.dataFormat;
    case 'formImage':
      return card.controls.dataFormat;
    case 'formAddress':
      return card.controls.dataFormat;
    case 'formFormula':
      return card.controls.dataFormat;
    case 'formHandSign':
      return card.controls.dataFormat;
    default:
      return true;
  }
};

export const GetDataTitleOptions = (formCards: FormCard[]) => {
  const filterCards = GetAllCard(formCards).filter((it: any) =>
    ['basic', 'system'].includes(it.group),
  );
  const list = filterCards.map((it: any) => {
    const displayName = it.controls.displayName;
    const label = displayName?.value ?? displayName.defaultValue;
    return {
      label,
      value: it.cardCode,
    };
  });
  return list;
};
